<?php

namespace Mem;

use MemLib\MemBase;
use MemLib\Pdb;

/**
 * 用户信息表
 *
 * @version 1.0
 * @author 张云江
 */
class MemOrder_User_info extends MemBase
{
    public function __construct()
    {
        parent::__construct(Pdb::db_order, "t_order_user_info");
    }

    /**
     * 插入数据
     * @param mixed $application_id t_order_application_info 应用ID
     * @param mixed $device_id 手机设备ID
     * @param mixed $device_name 手机设备名称
     * @param mixed $device_model 手机设备型号
     * @param mixed $vip 会员情况 1.会员 其余非会员
     * @param mixed $product_id 当前购买的产品ID
     * @param mixed $vip_time 当前会员到期时间
     * @param mixed $transaction_id 当前会员订单号
     */
    public function insertDataWithParam($application_id, $device_id, $device_name, $device_model, $vip = 0, $product_id = '', $vip_time = '', $transaction_id = '')
    {
        $vip = (int)$vip;
        $insert_dt = date("Y-m-d H:i:s", time());
        $data = array(
            "application_id" => $application_id,
            "device_id" => $device_id,
            "device_name" => $device_name,
            "device_model" => $device_model,
            "vip" => $vip,
            "product_id" => $product_id,
            "vip_time" => $vip_time,
            "transaction_id" => $transaction_id,
            "login_time" => $insert_dt,
            "insert_dt" => $insert_dt
        );
        $res = $this->addData($data);
        if ($res) {
            \Mlog::info("t_order_user_info 插入数据成功" . $device_id);
        }
        return $this->getTmpID();
    }

    /**
     * 根据订单详情
     * @param double $device_id
     * @return array
     */
    public function getInfoWithdevice_id($device_id)
    {
        $sql = "SELECT * FROM t_order_user_info WHERE device_id='{$device_id}';";
        $data = $this->getData($sql, "getInfoWithdevice_id:{$device_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 更新信息
     * @param $updata
     * @param $wheredata
     * @return bool
     */
    public function updateDataWithParam($updata, $wheredata)
    {
        if (empty($updata) || empty($wheredata)) {
            return false;
        }

        $updata = \MemLib\PublicFunc::convertToHtmlKeyVal($updata);
        $wheredata = \MemLib\PublicFunc::convertToHtmlKeyVal($wheredata);

        $ret = $this->updateData($updata, $wheredata);

        return $ret;
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getCountDataWithAll($application_id)
    {
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND application_id = ' . $application_id;
        }
        $sql = "SELECT COUNT(*) AS total FROM t_order_user_info WHERE user_id != 0 {$whereStr};";
        $data = $this->getData($sql, "getCountDataWithAllapplication_id:{$application_id}");
        return empty($data) ? $data : $data[0]['total'];
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getDataWithAll($application_id, $mempage)
    {
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND pcl.application_id = ' . $application_id;
        }
        $sql = "SELECT pcl.*,pclm.application_name FROM db_order.t_order_user_info AS pcl LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.application_id = pcl.application_id WHERE pcl.user_id != 0 {$whereStr} order by pcl.login_time desc LIMIT {$mempage->getLimitIndex()},{$mempage->pagesize};";
        $data = $this->getData($sql, "getDataWithAllapplication_id:{$application_id}:AndPage:{$mempage->getLimitIndex()}:pagesize:{$mempage->pagesize}");
        return $data;
    }
}
